package com.fafica.filtro;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fafica.entidades.InscritoMinicurso;
import com.fafica.util.Parametros;


/**
 * Servlet Filter implementation class FiltroUsuario
 */
@WebFilter(urlPatterns={"/submeter_resumo.jsp", "/boleto.jsp"})
public class FiltroUsuario implements Filter {

    public FiltroUsuario() {
        // TODO Auto-generated constructor stub
    }

    public void destroy() {
		// TODO Auto-generated method stub
	}

	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) arg0;
		HttpServletResponse response = (HttpServletResponse) arg1;
		HttpSession sessao = request.getSession();
		
		try{
			
			InscritoMinicurso usuario = (InscritoMinicurso) sessao.getAttribute(Parametros.USUARIO_LOGADO);
			if(usuario != null){
				chain.doFilter(arg0, arg1);
			}
			else{
				response.sendRedirect("login_submit.jsp");
			}			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public void init(FilterConfig fConfig) throws ServletException {
		
	}

}
